package com.gduf.lsm.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.gduf.lsm.common.constant.ActivityStatusConstant;
import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.entity.Activity;
import com.gduf.lsm.entity.JoinActivity;
import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.ActivityMapper;
import com.gduf.lsm.mapper.JoinActivityMapper;
import com.gduf.lsm.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author lsm
 * @Data
 */
@Service
public class ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private JoinActivityMapper joinActivityMapper;

    /**
     * 新增
     * @param activity
     */
    public void add(Activity activity) {
        //根据名字查询活动名是否重复
        if(activity.getName()==null){
            throw new CustomException(ResultCodeEnum.ACTIVITY_NULL_ERROR);
        }
        Activity dbActivity = activityMapper.selectByName(activity.getName());
        if (ObjectUtil.isNotNull(dbActivity)) {
            throw new CustomException(ResultCodeEnum.ACTIVITY_EXIST_ERROR);
        }
        activityMapper.insert(activity);
    }

    /**
     * 删除单个
     * @param id
     */
    public void deleteById(Integer id) {
        activityMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            activityMapper.deleteById(id);
        }
    }

    /**
     * 修改
     * @param activity
     */
    public void updateById(Activity activity) {
        activityMapper.updateById(activity);
    }

    /**
     * 根据ID查询
     * 填充活动状态
     * @param id
     * @return
     */
    public Activity selectById(Integer id) {
        Activity activity=activityMapper.selectById(id);

        //先判断是否开始，再判断是否人满，再判断是否报名，再判断是否结束
        Date date=new Date();
        String start=activity.getStart();
        //开始日期在当前日期之后，未开始
        if(DateUtil.parseDate(start).isAfter(date)){
            activity.setStatus(ActivityStatusConstant.ACTIVITY_STATUS_NOT_START);
        }else {
            //否则活动已开始判断是否人满再判断是否报名
            //判断人数是否已满
            Integer n=joinActivityMapper.selectJoinByActId(id);
            int joinNum;
            if(ObjectUtil.isNull(n)){
                joinNum=0;
            }else {
                joinNum=n;
            }
            activity.setNum(joinNum);
            if(joinNum<=activity.getJoinNum()){
                //判断是否报名
                Integer userId= TokenUtils.getCurrentUser().getId();
                JoinActivity joinActivity=joinActivityMapper.selectByActIdAndUserId(id,userId);
                if(ObjectUtil.isNotNull(joinActivity)){
                    activity.setStatus(ActivityStatusConstant.ACTIVITY_STATUS_SIGNED);
                }else {
                    activity.setStatus(ActivityStatusConstant.ACTIVITY_STATUS_NOT_SIGNED);
                }
            }else {
                activity.setStatus(ActivityStatusConstant.ACIVITY_JOIN_ALL);
            }
        }
        //在上面都判断结束后再判断是否结束
        String end=activity.getEnd();
        //结束日期在当前日期之前，已结束
        if(DateUtil.parseDate(end).isBefore(date)){
            activity.setStatus(ActivityStatusConstant.ACTIVITY_STATUS_OVER);
        }

        return activity;
    }

    /**
     * 查询所有
     * @param activity
     * @return
     */
    public List<Activity> selectAll(Activity activity) {
        return activityMapper.selectAll(activity);
    }

    /**
     * 分页查询
     * @param activity
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Activity> selectPage(Activity activity, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
//        Activity activity=new Activity();
//        activity.setAddress(activityPageQueryDTO.getAddress());
//        activity.setDescr(activityPageQueryDTO.getDescr());
//        activity.setName(activityPageQueryDTO.getName());
//        System.out.println("********"+activityPageQueryDTO);
//        System.out.println("*********"+activity);
        System.out.println("*********"+activity);
        List<Activity> list = activityMapper.selectAll(activity);
        return PageInfo.of(list);
    }
}
